<?php

    //Listing Table Class define
    include("ClassListing.php");
    //Listing Table Class define
    include("ClassUnit.php");
	//Sub Table Class define
    include_once("ClassSub.php");
	
class MultiFamilyClass
{
    var $Listing;
	var $GrossIncome;
	var $Expenses;
	var $NetIncome;
	var $VacancyFactor;
	var $Subdivision;
	var $AssociationFee;
	var $LivingArea;
	var $LotSize;
	var $Parking;
	var $Stories;
	var $YearBuilt;
	var $Heating;
	var $Cooling;
	var $Pool;
	var $InteriorFeatures;
	var $ExteriorFeatures;
	var $Style;
	var $Type;
	var $Roof;
	var $Exterior;
	var $UnitList;
	
	function AddItemFromXML( $xmlFormat )
	{
		$Array = $xmlFormat->children();

		$this->Listing = new ListingClass;
		$this->Listing->AddItemFromXML($Array[0]);
		
	 	$this->GrossIncome = new CurrencyFormatClass;
		$this->GrossIncome->AddItemFromXML($Array[2]);
	 	$this->Expenses = new CurrencyFormatClass;
		$this->Expenses->AddItemFromXML($Array[3]);
	 	$this->NetIncome = new CurrencyFormatClass;
		$this->NetIncome->AddItemFromXML($Array[4]);
		$this->VacancyFactor = $Array[5];
		$this->Subdivision = $Array[6];		
	 	$this->AssociationFee = new CurrencyFormatClass;
		$this->AssociationFee->AddItemFromXML($Array[7]);
		
		$this->LivingArea = new LivingAreaClass;
		$this->LivingArea->AddItemFromXML($Array[8]);
		$this->LotSize = new AreaRoomFormatClass;
		$this->LotSize->AddItemFromXML($Array[9]);
		$this->Parking = new ParkingClass;
		$this->Parking->AddItemFromXML($Array[10]);
		
		$Stories = $Array[11];	
		$YearBuilt = $Array[12];	
		$Heating = $Array[13];	
		$Cooling = $Array[14];	
		$Pool = $Array[15];	
		$InteriorFeatures = $Array[16];	
		$ExteriorFeatures = $Array[17];	
		$Style = $Array[18];	
		$Type = $Array[19];	
		$Roof = $Array[20];	
		$Exterior = $Array[21];	
		
		for ( $i = 22; $i <  count( $Array ) ; $i++ )
		{
			$Temp = new UnitClass;
			$Temp->AddItemFromXML($Array[$i]);
			$this->UnitList[] = $Temp;
		}		
	}
	
	function InsertSQL()
	{
		$strSqlCommand = "SELECT * FROM MultiFamily ";
		$strSqlCommand .= " WHERE Listing_idListing = '".$this->Listing->ListingID."' ";
		$result = mysql_query($strSqlCommand);
		if( !$result )
		{
			echo $this->Listing->ListingID.$strSqlCommand." Search Failed<br/>";
		}
		if( @mysql_num_rows($result) == 0)
		{	// New
			$this->Listing->InsertSQL();
			$this->Expenses->InsertSQL();		
			$this->GrossIncome->InsertSQL();		
			$this->NetIncome->InsertSQL();		
			$this->AssociationFee->InsertSQL();
			$this->LivingArea->InsertSQL();		
			$this->LotSize->InsertSQL();		
			$this->Parking->InsertSQL();		
			
			$strSqlCommand = "INSERT INTO MultiFamily( Listing_idListing, GrossInCome, ";
			$strSqlCommand .="Expenses, NetIncome, VacancyFactor, Subdivision, ";
			$strSqlCommand .="AssociationFee, LivingArea_idLivingArea, LotSize, ";
			$strSqlCommand .="Parking_idParking, Stories, YearBuilt, Heating, Cooling, ";
			$strSqlCommand .="Pool, InteriorFeatures, ExteriorFeatures, Style, Type, ";
			$strSqlCommand .="Roof, Exterior) ";
			$strSqlCommand .= "VALUES( '".$this->Listing->ListingID."', ";
			$strSqlCommand .= "'".$this->GrossIncome->ID."', ";
			$strSqlCommand .= "'".$this->Expenses->ID."', ";
			$strSqlCommand .= "'".$this->NetIncome->ID."', ";
			$strSqlCommand .= "'".$this->VacancyFactor."', ";
			$strSqlCommand .= "'".$this->Subdivision."', ";
			$strSqlCommand .= "'".$this->AssociationFee->ID."', ";
			$strSqlCommand .= "'".$this->LivingArea->ID."', ";
			$strSqlCommand .= "'".$this->LotSize->ID."', ";
			$strSqlCommand .= "'".$this->Parking->ID."', ";
			$strSqlCommand .= "'".$this->Stories."', ";
			$strSqlCommand .= "'".$this->YearBuilt."', ";
			$strSqlCommand .= "'".$this->Heating."', ";
			$strSqlCommand .= "'".$this->Cooling."', ";
			$strSqlCommand .= "'".$this->Pool."', ";
			$strSqlCommand .= "'".$this->InteriorFeatures."', ";
			$strSqlCommand .= "'".$this->ExteriorFeatures."', ";
			$strSqlCommand .= "'".$this->Style."', ";
			$strSqlCommand .= "'".$this->Type."', ";
			$strSqlCommand .= "'".$this->Roof."', ";
			$strSqlCommand .= "'".$this->Exterior."') ";
				
			if(!mysql_query($strSqlCommand))
			{
				//echo $this->Listing->ListingID.$strSqlCommand." Insert Failed<br/>";
			}
			$id = mysql_insert_id();
			
			foreach ( $this->UnitList as $Unit )
			{
				$Unit->InsertSQL( $id );;
			}

		}
		else
		{	// Modify
			$rowMF = @mysql_fetch_array($result);

			$strSqlCommand =  "UPDATE MultiFamily SET ";
			$strSqlCommand =  "VacancyFactor='"	.$this->VacancyFactor	."', ";
			$strSqlCommand .= "Subdivision='"	.$this->Subdivision		."', ";
			$strSqlCommand .= "Stories='".$this->Stories."', ";
			$strSqlCommand .= "YearBuilt='".$this->YearBuilt."', ";
			$strSqlCommand .= "Heating='".$this->Heating."', ";
			$strSqlCommand .= "Cooling='".$this->Cooling."', ";
			$strSqlCommand .= "Pool='".$this->Pool."', ";
			$strSqlCommand .= "InteriorFeatures='".$this->InteriorFeatures."', ";
			$strSqlCommand .= "ExteriorFeatures='".$this->ExteriorFeatures."', ";
			$strSqlCommand .= "Style='".$this->Style."', ";
			$strSqlCommand .= "Type='".$this->Type."', ";
			$strSqlCommand .= "Roof='".$this->Roof."', ";
			$strSqlCommand .= "Exterior='".$this->Exterior."' ";
			$strSqlCommand .= "' WHERE idMultiFamily = ".$rowMF["idMultiFamily"];
			if(!mysql_query($strSqlCommand))
			{
				echo $this->Listing->ListingID. $strSqlCommand." Modify Failed<br/>";
			}
			
			$this->Listing->ModifySQL(  );
			$this->GrossIncome->ModifySQL( $rowMF["GrossInCome"] );
			$this->Expenses->ModifySQL( $rowMF["Expenses"] );
			$this->NetIncome->ModifySQL( $rowMF["NetIncome"] );
			$this->AssociationFee->ModifySQL( $rowMF["AssociationFee"] );
			$this->LivingArea->ModifySQL( $rowMF["LivingArea_idLivingArea"] );
			$this->LotSize->ModifySQL( $rowMF["LotSize"] );
		}
	}
}

?>